<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Image;

class Up extends Controller
{
    //文件上传界面显示
    public function index()
    {

        return view('admin@/up/index');
    }

    //文件上传接收
    public function upload(Request $req)
    {
        //获取表单上传文件
        $file = $req->file('pic');
        //移动到框架应用根目录
        $info = $file->move('./uploads');
        if ($info) {
            $pathname = substr(str_ireplace('\\', '/', $info->getPathName()), 1);

            //图片裁切
            //打开图片
            $imag = Image::open(public_path().$pathname);
            //生成缩列图
            $imag->thumb(150,150)->save(public_path().$pathname);
            return json(['status' => 0, 'msg' => $pathname]);
        } else {
            return json(['status' => 1, 'msg' => $file->getError()]);
        }
    }

    //删除文件
    public function del(Request $req)
    {
        $img = public_path().$req->delete('img');


        if (unlink($img)) {

            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}
